﻿using Koala.Pro.Common;
using Koala.Pro.Core;
using Koala.Pro.Core.Dto;
using Koala.Pro.Core.Models;
using SqlSugar;

namespace Koala.Pro.Services
{
    public class OperationLogService : BaseCrudService<OperationLogInfo, string, OperationLogPagedDto>, IOperationLogService, IMyCrudService<OperationLogInfo, string, OperationLogPagedDto>, ITransientDependency, IDependency
    {
        public OperationLogService(IApiUserSession currentApiUser)
        {
            base.CurrentApiUser = currentApiUser;
        }

        public override Task<Dictionary<string, string>> GetColumnNameAlias()
        {
            return Task.FromResult(new Dictionary<string, string>
        {
            { "User_ID", "登录用户ID" },
            { "LoginName", "登录名" },
            { "FullName", "真实名称" },
            { "Company_ID", "所属公司ID" },
            { "CompanyName", "所属公司名称" },
            { "TableName", "操作表名称/实体类全名" },
            { "OperationType", "操作类型" },
            { "Note", "日志描述" },
            { "IPAddress", "IP地址" },
            { "MacAddress", "Mac地址" },
            { "CreateTime", "创建时间" }
        });
        }

        protected override ISugarQueryable<OperationLogInfo> CreateFilteredQueryAsync(OperationLogPagedDto input)
        {
            return base.CreateFilteredQueryAsync(input).WhereIF(!input.ExcludeId.IsNullOrWhiteSpace(), (OperationLogInfo t) => t.Id != input.ExcludeId).WhereIF(!input.User_ID.IsNullOrWhiteSpace(), (OperationLogInfo t) => t.User_ID.Contains(input.User_ID))
                .WhereIF(!input.LoginName.IsNullOrWhiteSpace(), (OperationLogInfo t) => t.LoginName.Contains(input.LoginName))
                .WhereIF(!input.FullName.IsNullOrWhiteSpace(), (OperationLogInfo t) => t.FullName.Contains(input.FullName))
                .WhereIF(!input.Company_ID.IsNullOrWhiteSpace(), (OperationLogInfo t) => t.Company_ID.Equals(input.Company_ID))
                .WhereIF(!input.CompanyName.IsNullOrWhiteSpace(), (OperationLogInfo t) => t.CompanyName.Contains(input.CompanyName))
                .WhereIF(!input.TableName.IsNullOrWhiteSpace(), (OperationLogInfo t) => t.TableName.Contains(input.TableName))
                .WhereIF(!input.OperationType.IsNullOrWhiteSpace(), (OperationLogInfo t) => t.OperationType.Contains(input.OperationType))
                .WhereIF(!input.Note.IsNullOrWhiteSpace(), (OperationLogInfo t) => t.Note.Contains(input.Note))
                .WhereIF(!input.IPAddress.IsNullOrWhiteSpace(), (OperationLogInfo t) => t.IPAddress.Contains(input.IPAddress))
                .WhereIF(!input.MacAddress.IsNullOrWhiteSpace(), (OperationLogInfo t) => t.MacAddress.Contains(input.MacAddress))
                .WhereIF(input.CreateTimeStart.HasValue, (OperationLogInfo s) => s.CreateTime >= input.CreateTimeStart.Value)
                .WhereIF(input.CreateTimeEnd.HasValue, (OperationLogInfo s) => s.CreateTime <= input.CreateTimeEnd.Value);
        }

        protected override ISugarQueryable<OperationLogInfo> ApplyDefaultSorting(ISugarQueryable<OperationLogInfo> query)
        {
            return query.OrderBy((OperationLogInfo s) => s.CreateTime, OrderByType.Desc);
        }

        public virtual async Task<OperationLogSettingInfo> GetOperationLogSetting(string tablename)
        {
            return await GetEntity<OperationLogSettingInfo>().GetSingleAsync((OperationLogSettingInfo s) => s.TableName == tablename && s.Forbid == 0);
        }

        public virtual async Task<bool> AddOperationLog(string tableName, string operationType, string note)
        {
            OperationLogInfo operationLogInfo = new OperationLogInfo(base.CurrentApiUser, tableName, operationType, note);
            SetIdForGuids(operationLogInfo);
            return await GetEntity<OperationLogInfo>().InsertAsync(operationLogInfo);
        }
    }
}
